package com.corn.juc.lock;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function : 证明 synchronized 为可重入锁
 * @since : 2020/12/28 15:19
 */

public class SynchronizedReentrantDemo {

    static class TestDemo {
        public synchronized void method1() throws InterruptedException {
            System.out.println(Thread.currentThread().getName() + " invoke method1 ...");
            TimeUnit.SECONDS.sleep(1);
            method2();
        }

        public synchronized void method2() {
            System.out.println(Thread.currentThread().getName() + " invoke method2 ...");
        }
    }


    public static void main(String[] args) {
        TestDemo testDemo = new TestDemo();
        new Thread() {
            @SneakyThrows
            @Override
            public void run() {
                testDemo.method1();
            }
        }.start();
        new Thread() {
            @SneakyThrows
            @Override
            public void run() {
                testDemo.method1();
            }
        }.start();
    }
}
